package com.zpself.module.算法练习.计算;

import java.util.*;

/**
 * 你有 4 张写有 1 到 9 数字的牌。你需要判断是否能通过 *，/，+，-，(，) 的运算得到 24。
 * @author By ZengPeng
 * @Description
 * @date in  2020/9/27 15:27
 * @Modified By
 */
public class 力扣_679_24点游戏 {

    private static int index=0;

    public static void main(String[] args) {
        Random random = new Random();
        int[] target = new int[4];
        for (int i = 0; i <target.length; i++) {
            target[i] = random.nextInt(9)+1;
            System.out.print(target[i]+",");
        }
        System.out.println("结果如下------------------------------");
        if(haveProbably(target))
            System.out.println("有");
        else
            System.out.println("无");

    }

    public static boolean haveProbably(int[] target){
        //两两处理
        //【1与2】，【1与3】，【1与4】
        //集合1、集合2
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        List<Integer> targetList = new ArrayList<>();
        for (int aTarget : target) {
            targetList.add(aTarget);
        }

        for (int i = 1; i < target.length; i++) {
            Integer team1_a = target[0];
            Integer team1_b = target[i];

            List<Integer> currentList = new ArrayList<>(targetList);
            currentList.remove(team1_a);
            currentList.remove(team1_b);
            Integer team2_a  =currentList.get(0);
            Integer team2_b  =currentList.get(1);
            addElement(set1, team1_a, team1_b);
            addElement(set2, team2_a, team2_b);
            for (Integer t1 : set1) {
                for (Integer t2 : set2) {
                    if(t1 + t2 == 24 || t1 - t2 == 24 || t2 - t1 == 24 || t1 * t2 == 24
                            || (t2!=0 && t1 % t2==0 && t1 / t2 == 24) || (t1!=0 && t2 % t1==0 && t2 / t1 == 24)) {
                        System.out.print("成功方案"+(++index)+":"+"["+team1_a+","+team1_b+"], "+currentList.toString()+" 组合值为：");
                        System.out.println(t1+","+t2);
                    }
                }
            }
            set1.clear();
            set2.clear();
        }

        //三一处理
        Set<Integer> set3 = new HashSet<>();
        Set<Integer> set4 = new HashSet<>();
        for (int i = 0; i < target.length; i++) {
            Integer alone = target[i];
            List<Integer> currentList = new ArrayList<>(targetList);
            currentList.remove(alone);
            //3个中先挑一个出来，其它两个组合，得到的值再与挑出来的值组合
            for (Integer integer : currentList) {
                List<Integer> currentList2 = new ArrayList<>(currentList);
                currentList2.remove(integer);
                addElement(set3,currentList2.get(0),currentList2.get(1));
                for (Integer zhuHeZhi : set3) {
                    addElement(set4,zhuHeZhi,integer);
                }
                set3.clear();
            }
            for (Integer abc : set4) {
                if(abc + alone == 24 || alone - abc == 24 || abc - alone == 24 || alone * abc == 24
                        || (abc!=0 && alone % abc==0 && alone / abc == 24) || (alone!=0 && abc % alone==0 && abc / alone == 24)) {
                    System.out.print("成功方案"+(++index)+":"+"["+alone+"], "+currentList.toString()+" 组合值为：");
                    System.out.println(alone+","+abc);
                }
            }

            set4.clear();
        }
        return index>0;
    }


    private static void addElement(Set<Integer> set1, Integer team1_a, Integer team1_b) {
        set1.add(team1_a+team1_b);
        set1.add(team1_a-team1_b);
        set1.add(team1_b-team1_a);
        set1.add(team1_a*team1_b);
        if(team1_b!=0 && team1_a%team1_b==0)
            set1.add(team1_a/team1_b);
        if(team1_a!=0 && team1_b%team1_a==0)
            set1.add(team1_b/team1_a);
    }
}
